package com.teach.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teach.entity.ResponseEntity;
import com.teach.entity.TbIntegrate;
import com.teach.entity.TbPrize;
import com.teach.mapper.TbPrizeMapper;
import com.teach.service.TbIntegrateService;
import com.teach.mapper.TbIntegrateMapper;
import com.teach.utils.LotteryUtils;
import com.teach.utils.RedisUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 *
 */
@Service
public class TbIntegrateServiceImpl extends ServiceImpl<TbIntegrateMapper, TbIntegrate>
    implements TbIntegrateService{
    @Resource
    TbIntegrateMapper tbIntegrateMapper;
    @Resource
    TbPrizeMapper tbPrizeMapper;
    /* @Resource
     Redisson redisson;*/
    @Resource
    RedisUtils redisUtils;
    @Resource
    LotteryUtils lotteryUtils;

    @Override
    public ResponseEntity getWard(Integer userId) {
        int countUserId=0;
        int lucky=15001;
         /*RLock lock = redisson.getLock(userId+"");
        lock.lock();
        //自定义时间加锁
        lock.lock(3, TimeUnit.SECONDS);*/
        // try {
        //根据用户的id查询积分
        TbIntegrate tbIntegrate = tbIntegrateMapper.selectById(userId);
        //判断积分是否足够，足够的话可以抽奖，不够的话不能抽奖
        if(tbIntegrate.getIntegrateNum()<100){
            return new ResponseEntity(null,500,"积分不够，不能抽奖",false);
        }
        //将每个用户的抽奖次数放到redis中
        redisUtils.hIncream("lotteryCount","count"+userId);
        //查询用户抽奖几次了
        Object lotteryCount = redisUtils.hget("lotteryCount", "count" + userId);
        if(lotteryCount !=null){
            if(lotteryCount instanceof Integer){
                countUserId=(int)lotteryCount;
            }
        }
    //抽奖返回时让redis自增一，目的是让抽奖人数加一
    redisUtils.hIncream("lottery","count");
        if(countUserId>3){
            //抽奖次数大于3，提示不允许在抽奖了
            return new ResponseEntity(null,500,"抽奖次数大于3，不允许在抽奖了",false);
        }else{
            //抽奖次数小于三，根据抽奖的次数来修改积分
            TbIntegrate integrate = new TbIntegrate();
            integrate.setId(tbIntegrate.getId());
            int num=tbIntegrate.getIntegrateNum()-100;
            integrate.setIntegrateNum(num);
            integrate.setUserId(userId);
            //修改积分
            tbIntegrateMapper.updateById(integrate);
        }
        //判断是第几个用户在抽奖
            Integer count  = (Integer) redisUtils.hget("lottery","count");
            //根据当前用户所对应的个数去判断奖品(这个判断是否是大奖)
            TbPrize tbPrize = tbPrizeMapper.selectLuky(count);
            if(tbPrize!=null){
                if(tbPrize.getLucky().intValue()==lucky){
                    //不为空是大奖
                    return new ResponseEntity(tbPrize.getPrize(),200,"恭喜你抽到iphone",true);
                }
            }
                //为空是小奖品(积分)
                Integer integral = lotteryUtils.BackIntegral();
                //根据当前用户的id把抽到的积分加入到所对应的积分表中
                TbIntegrate newTbIntegrate = tbIntegrateMapper.selectById(userId);
                TbIntegrate newIntegrate = new TbIntegrate();
                newIntegrate.setId(newTbIntegrate.getId());
                newIntegrate.setIntegrateNum(newTbIntegrate.getIntegrateNum()+integral);
                //修改
                tbIntegrateMapper.updateById(newIntegrate);
                return new ResponseEntity(integral,200,"恭喜你抽中积分",true);
        //}finally {
        //    lock.unlock();
        //}
    }
}




